package postpc.powerreminders.com.ui.activities;

import postpc.powerreminders.com.db.RotgDatabase;

import postpc.powerreminders.com.R;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RemindersActivity extends ListActivity {
	
	Cursor m_AllReminders;
	
	private static final String TAG = "RemindersActivity";
	
	private void reloadReminders() {
		
		
		
		RotgDatabase db = RotgDatabase.getInstance();
		m_AllReminders = db.fetchAllReminders();

		ListView listView = getListView();
		
		
		CursorAdapter adapter = new ItemAdapter(getApplicationContext(), m_AllReminders);
				
		listView.setAdapter(adapter);

		db.close();
	}
		
	
	private void editReminder(long reminderId) {
		Intent editReminderIntent = new Intent();
		editReminderIntent.setClassName(getApplicationContext(), ReminderEditActivity.class.getName());
		editReminderIntent.putExtra("id", reminderId);
		Log.d(TAG, "Opening edit for reminder id="+reminderId);
		startActivityForResult(editReminderIntent, 0);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		reloadReminders();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		reloadReminders();
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.d("IMPORTANT", "On Create");
		
		
		ListView listView = getListView();

		registerForContextMenu(listView);

		
		// Get rid of the annoying text filter
		listView.setTextFilterEnabled(false);
		
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
				editReminder(id);
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.reminders_menu, menu);
		return true;
	}
	
	private void newReminder() {
		Intent newReminderIntent = new Intent();
		newReminderIntent.setClassName(getApplicationContext(), ReminderEditActivity.class.getName());
		newReminderIntent.setAction(Intent.ACTION_EDIT);
		startActivityForResult(newReminderIntent, 0);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_reminder_menu_item:
			newReminder();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		reloadReminders();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		
		if (v.getId() == getListView().getId()) {
			menu.add(0, 0, 0, "Edit");
			menu.add(0, 1, 1, "Delete");
			
			menu.add(0, 2, 2, "Activate");
			menu.add(0, 3, 3, "Deactivate");
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		
		RotgDatabase db = RotgDatabase.getInstance();
		
		Log.d("IMPORTANT", "menu item index = " + menuItemIndex + " For reminder_id = " + info.id);
		if (menuItemIndex == 0) {
			// Edit
			editReminder(info.id);
		}
		else if (menuItemIndex == 1) {
			// Delete
			db.deleteReminder(info.id);
			
			reloadReminders();
		}
		else if (menuItemIndex == 2) {
			// Activate
			db.activateReminder(info.id);
			
			reloadReminders();
		}
		else if (menuItemIndex == 3) {
			// Deactivate
			db.deactivateReminder(info.id);
			
			reloadReminders();
		}

		return true;

	}
	
	private class ItemAdapter extends CursorAdapter {
	    private LayoutInflater mLayoutInflater;
	    
	    public ItemAdapter(Context context, Cursor c) {
	        super(context, c);
	        
	        mLayoutInflater = LayoutInflater.from(context); 
	    }

	    @Override
	    public View newView(Context context, Cursor cursor, ViewGroup parent) {
	        View v = mLayoutInflater.inflate(R.layout.reminder_item, parent, false);
	        return v;
	    }


	    @Override
	    public void bindView(View v, Context context, Cursor c) {
	        String name = c.getString(c.getColumnIndexOrThrow(RotgDatabase.NAME));
	        boolean isActive = c.getInt(c.getColumnIndexOrThrow(RotgDatabase.ACTIVE)) != 0;
	        
	        TextView nameText = (TextView)v.findViewById(R.id.reminder_item_name);
	        View redImage = v.findViewById(R.id.reminder_item_red_image);
	        View greenImage = v.findViewById(R.id.reminder_item_green_image);
	        
	        nameText.setText(name);
	        
	        if (isActive) {
	        	greenImage.setVisibility(View.VISIBLE);
	        }
	        else {
	        	redImage.setVisibility(View.VISIBLE);
	        }

	    }
	}

}
